<?php
	
	class tunghoanh extends story{
		
		public $patternUrl; 
		public $startPage = 1;
		
		public function getChapters($storyUrl){

			$model = new Model();
			$pos_start = strpos($storyUrl, '.html1');
			if($pos_start !== false){
			
				$storyUrl = str_replace('.html1', '.html', $storyUrl);
			}else{
				//echo $storyUrl;
			}

			$html = file_get_html($storyUrl);
			
			if(!$html){
				echo 'het';
				exit();
			}
			
			$i = 0;
			
			$main = $html->find('.tr');
			if(!$main){ 
				echo 'het';
				exit();
			}
			
			$countChapter = count($main);
			
			$i = $countChapter - 1;
			
			//$main = $html->find('.tr');
			
			$indexChuong = 2;
			$indexTenChuong = 3;
			
			$patternTap = '/(Tập|Quyển) ([\d]*)/i';
			$patternChapt = '/(Chương) ([\d]*)/i';
			
			for($i; $i <= $countChapter; $i++){
				$title =  $main[$i]->children(0)->children(0)->innertext . '<br />';
				
				$chuongLink =  $main[$i]->children(0)->children(0)->attr['href'];
				
				preg_match($patternTap, $wholeTitle, $matcheTap);
			
				preg_match($patternChapt, $wholeTitle, $matcheChapt);
			
				if($matcheTap[2]){
					$tap = trim($matcheTap[2]);
					$_SESSION['tap'] = $tap;
				}else{
					$tap = $_SESSION['tap'];
				}
				
				if($matcheChap[2]){
					$matchChap = 'true';
					$chapt = (int) trim($matcheChap[2]);
					$_SESSION['last_chapt'] = $chapt;
				}else{
					/*
					 * Trường hợp không lấy được chap và link
					* vẫn lấy chapt bằng $_SESSION['last_chapt'] để sau này update bằng tay
					*/
					$matchChap = 'false';
					$chapt = (int) $_SESSION['last_chapt'] + 1;
					$_SESSION['chapts'][] = $chapt;
				}
				
				$_SESSION['last_chapt'] = $chapt;
				
				$index = $tap . '_' . $chapt;
				
				if($chuongLink){
				
					$links[$index] = array(
							'link' => $chuongLink,
							'title'=> $title,
							'tap'  => $tap,
							'chapt' => $chapt,
							//'session_last_chapt' => $_SESSION['last_chapt'],
							//'session_chapts'=>$_SESSION['chapts'],
							//'matchChap'	=> $matchChap,
							//'in_array'	=> $in_array,
								
					);
				}else{
					//Kiểm tra nếu có trường hợp Dom lỗi sẽ không lấy được attr
					// VD: <a title="Ví dụ "đầu tiên"" href="">
					$links[$index] = array('title'=> 'Error', 'href'=> 'Error');
				}
			}
			
			$html->__destruct();
			unset($ret);
			
			return $links;
			
		}
		
		public function getChapterContent($href){
			echo '<pre>';
			print_r($href);
			echo '</pre>';
			set_time_limit(7200);
			$pos = strpos($href, 'http://');
			if($pos !== false){
				
				$a = 'var page = require(\'webpage\').create();

page.onConsoleMessage = function(msg) {
    console.log(msg);
};

page.open("'.$href.'", function(status) {
    if ( status === "success" ) {
        page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", function() {
            page.evaluate(function() {
            	
                console.log($("#chapter_content").html());
            });
            phantom.exit();
        });
    }
});';
				file_put_contents('phantomwebintro.js', $a);
				exec('phantomjs phantomwebintro.js', $a, $b);

				$count = count($a);
				$content_html = $a[$count - 1];

				//$html = file_get_html($href);
				//$content_html = $html->find('#chapter_content', 0)->innertext;
				//$html->__destruct();
				
				return $content_html;
			}else{
				return false;
			}
		
		}
		
	}